// 定义一个全局变量，方便设置默认值时获取tree对象
var globalTreeId = '#permission_tree';
var Permission = function () {

    this.$tree = $(globalTreeId);
    this.$grid_selector = $('#permission_grid_table');
    this.pager_selector = "#permission_grid_pager";

    this.URL = {
        GET_BY_MODULE_ID: '/member/permission/get/all/moduleId',
        PERMISSION_EDIT: '/member/permission/modify'
    };

    this.colModel = [
        {name: 'permissionId', index: 'permissionId', key: true, width: 50, editable: true},
        {
            name: 'moduleId',
            index: 'moduleId',
            width: 50,
            hidden: true,
            editable: true,
            editrules: {required: true},
            addHiddenControl: function () {
                return $(this).xumf_jqGird('getGridParam', 'postData')['searchString'];
            }
        },
        {
            name: 'name',
            index: 'name',
            width: 80,
            editable: true,
            editoptions: {size: "50", maxlength: "30"},
            editrules: {required: true}
        },
        {
            name: 'code',
            index: 'code',
            editable: true,
            editoptions: {
                defaultValue: this.codeDefaultValue,
                size: "50",
                maxlength: "100"
            },
            editrules: {required: true}
        },
        {
            name: 'sequence',
            index: 'sequence',
            width: 50,
            sortable: true,
            editable: true,
            editoptions: {size: "50", maxlength: "30"},
            editrules: {required: true, number: true}
        },
        {
            name: 'comment',
            index: 'comment',
            sortable: false,
            editable: true,
            edittype: "textarea",
            editrules: {required: true},
            editoptions: {
                size: "50"
            }
        },
        {name: 'createdAt', index: 'createdAt', width: 100},
        {name: 'updatedAt', index: 'updatedAt', width: 100}
    ];

    this.options = {
        url: this.URL.GET_BY_MODULE_ID,
        editurl: this.URL.PERMISSION_EDIT,
        datatype: 'local',
        pager: this.pager_selector,
        sortname: 'sequence',
        caption: "权限列表",
        colNames: ['ID', '模块ID', '名称', '编号', '顺序', '说明', '创建时间', '更新时间'],
        colModel: this.colModel

    };
};

Permission.prototype = {
    /**
     * 在添加权限时，设权限默认值
     * @returns string 默认值
     */
    codeDefaultValue: function () {
        // 当前tree选择的code
        var selectedItem = $(globalTreeId).xumf_tree.getSelectedItem($(globalTreeId));
        return selectedItem ? selectedItem.code + ':' : '';
    },
    remoteDataSource: function (options, callback) {
        if (!options.hasOwnProperty('dataAttributes')) { // 初始化树形时间，添加root菜单
            callback({
                data: [{name: '根模块', type: 'folder', dataAttributes: {id: $.Const.ROOT_ID}}]
            });
        } else {
            var id = options.dataAttributes.id;
            $.get('/member/module/tree/get/' + id).done(function (res) {
                callback({data: res.data});
            });
        }
    },
    initTree: function () {
        var $grid = this.$grid_selector;
        /**
         * 树形
         */
        this.$tree.xumf_tree({dataSource: this.remoteDataSource});
        // 默认打开顶级菜单
        // this.$tree.find('#' + $.Const.ROOT_ID) 这种写法为了防止重复id时，无法找到
        this.$tree.xumf_tree('openFolder', this.$tree.find('#' + $.Const.ROOT_ID));
        // 树形点击事件
        this.$tree.on('selected.fu.tree', function (event, data) {
            $grid.setGridParam({datatype: 'json'})
                .setGridParam({postData: {searchString: data.target.dataAttributes.id}})
                .trigger('reloadGrid');
        });
    },
    initGird: function () {
        this.$grid_selector.xumf_jqGird(this.options);
    }
};
